package com.xu.cloud.common.openapi;

import com.xu.cloud.common.api.XuApi;
import com.xu.cloud.common.conditional.ExcludeProfile;
import com.xu.cloud.common.constant.EnvConstant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger docket config
 *
 * @author xuguan
 * @since 2024/2/4
 */
@ExcludeProfile(EnvConstant.PROD)
@EnableSwagger2
public class OpenApi2DocketConfig {

    @Bean
    @ConditionalOnMissingBean
    public Docket openapi2Docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("xu-cloud-openapi2.0")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xu")
                        .and(RequestHandlerSelectors.withClassAnnotation(XuApi.class)
                                .or(RequestHandlerSelectors.withClassAnnotation(Controller.class))
                                .or(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                        ))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(ApiIgnore.class)
                .enableUrlTemplating(true);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XuCloud Springfox OpenApi2.0")
                .description("XuCloud接口文档")
                .termsOfServiceUrl("http://springfox.io")
                .contact(new Contact("xuguan", "https://xuguan.eu.org", "peacefish8@protonmail.com"))
                .license("Apache License Version 2.0")
                .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                .version("2.0")
                .build();
    }
}
